package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.AddCommentBean;
import cst.neu.yuejian_31.iface.AddCommentIface;
import cst.neu.yuejian_31.iface.AddCommentListener;
import cst.neu.yuejian_31.service.AddCommentService;
import cst.neu.yuejian_31.Common.Constant;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/24.
 */

public class AddCommentModel implements AddCommentIface {
    private Retrofit retrofit;
    public AddCommentModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void addComment(String postId, String userId, String commentText, final AddCommentListener addCommentListener) {
        AddCommentService addCommentService=retrofit.create(AddCommentService.class);
        Call<AddCommentBean>call=
                addCommentService.addComment(postId,userId,commentText);
        call.enqueue(new Callback<AddCommentBean>() {
            @Override
            public void onResponse(Call<AddCommentBean> call, Response<AddCommentBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    addCommentListener.onSuccess(response.body());

                }else {
                    addCommentListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<AddCommentBean> call, Throwable t) {
                addCommentListener.onFail(t.toString());

            }
        });
    }
}
